local desc_1v1 = [[
  # pokemon模式简介

  两人进行对战的竞技化模式，1v1对决。

  ___

  ## 游戏流程

  1.选将，获得初始buff，开始时体力上限调整为8。玩家回合仅调整为开始阶段-技能选择阶段-结束阶段

]]
local GameEvent=require "packages/jbs2/pokgame/pokeflow"
local Pe=require "packages/jbs2/pokgame/pokegegine"

-- FIXME: Disable same convert by param or extra data
local function rm(generals, g)
  local gt = Fk.generals[g].trueName
  for i, v in ipairs(generals) do
    if Fk.generals[v].trueName == gt then
      table.remove(generals, i)
      return
    end
  end
end


--
--技能获得
local function huanhua_Newskill(room,players)
  local room=room
  for _, player in ipairs(players) do
  local existingGenerals = {}
  table.forEach(room.players, function(p)
    table.insertTable(existingGenerals, {p.general, p.deputyGeneral})
  end)
  local generals = table.map(Fk:getGeneralsRandomly(10, Fk:getAllGenerals(), existingGenerals,
    (function(p) return false end)), function(g) return g.name end)
  generals = table.random(generals, math.min(3, #generals))
  local skills = {}
  local choices = {}
for _, general_name in ipairs(generals) do
local general = Fk.generals[general_name]
local g_skills = {}
--加载技能
for _, skill in ipairs(general.skills) do
  if not (table.contains({Skill.Limited, Skill.Wake, Skill.Quest}, skill.frequency) or skill.lordSkill) then
    table.insertIfNeed(g_skills, skill.name)
  end
end
for _, s_name in ipairs(general.other_skills) do
  local skill = Fk.skills[s_name]
  if not (table.contains({Skill.Limited, Skill.Wake, Skill.Quest}, skill.frequency) or skill.lordSkill)  then
    table.insertIfNeed(g_skills, skill.name)
  end
end
table.insertIfNeed(skills, g_skills)
if #choices == 0 and #g_skills > 0 then
  choices = {g_skills[1]}
end
end

if #choices > 0 then
  player.request_data = json.encode({
    path = "packages/tenyear/qml/ChooseGeneralSkillsBox.qml",
    data = {
      generals, skills, 1, 1, "jbsfire__reward",true
    },
  })
  player.default_reply = table.random(skills, 1)
end

--循环
end
room:doBroadcastRequest("CustomDialog", players)

for _, p in ipairs(players) do
  if p.id>0 then
  local choice = p.reply_ready and json.decode(p.client_reply) or {"jiang"}
  
  room:handleAddLoseSkills(p, table.concat(choice, "|") , nil)
  end
end

end

local function execGameEvent(type, ...)
  local event = GameEvent:new(type, ...)
  local _, ret = event:exec()
  return ret
end

--buff表
local bufflist={"jbsfire_liuanhuaming","jbsfire_mianmianjudao","jbsfire_xushidaifa","jbsfire_duoduoyishan"}

local m_1v1_getLogic = function()
  local m_1v1_logic = GameLogic:subclass("jbsaola_logic")
  function m_1v1_logic:prepareForStart()
    local room = self.room
    local players = room.players

    self:addTriggerSkill(GameRule)
    for _, trig in ipairs(Fk.global_trigger) do
      if trig.name ~= "mobile_effect" then  --关闭手杀特效
        self:addTriggerSkill(trig)
      end
    end
    self.room:sendLog{ type = "$GameStart" }
  end

  function m_1v1_logic:run()
    -- default logic
    local room = self.room
    table.shuffle(self.room.players)
    self:assignRoles()
    self.room.game_started = true
    room:doBroadcastNotify("StartGame", "")
    room:adjustSeats()
    --[[ 因为未完工，在release版暂时不启用。
    for _, p in ipairs(room.players) do
      p.ai = SmartAI:new(p)
    end
    --]]
    self:chooseGenerals()
    self:prepareDrawPile()
    self:buildPlayerCircle()
    self:broadcastGeneral()
    self:attachSkillToPlayers()
    self:prepareForStart()
    self:action()
  end
  function m_1v1_logic:action()
    self:trigger(fk.GamePrepared)
    local room = self.room
  
    while true do
      execGameEvent(GameEvent.PokeRound)
      if room.game_finished then break end
    end
  end
  function m_1v1_logic:chooseGenerals()
    local room = self.room
    local generalNum = room:getSettings('generalNum')
    local n = room:getSettings('enableDeputy') and 2 or 1
    local lord = room:getLord()
    local lord_generals = {}
  
    if lord ~= nil then
      room.current = lord
      room:setBanner("@[:]jbsfire_rule","jbsaola-intro")
    
    end
  
    local nonlord = room.players
    local generals = room:getNGenerals(#nonlord * generalNum)
    table.shuffle(generals)
    for i, p in ipairs(nonlord) do
      local arg = table.slice(generals, (i - 1) * generalNum + 1, i * generalNum + 1)
      p.request_data = json.encode{ arg, n }
      p.default_reply = table.random(arg, n)
    end
  
    room:notifyMoveFocus(nonlord, "AskForGeneral")
    room:doBroadcastRequest("AskForGeneral", nonlord)
  
    local selected = {}
    for _, p in ipairs(nonlord) do
      if p.general == "" and p.reply_ready then
        local general_ret = json.decode(p.client_reply)
        local general = general_ret[1]
        local deputy = general_ret[2]
        table.insertTableIfNeed(selected, general_ret)
        room:setPlayerGeneral(p, general, true, true)
        room:setDeputyGeneral(p, deputy)
      else
        room:setPlayerGeneral(p, p.default_reply[1], true, true)
        room:setDeputyGeneral(p, p.default_reply[2])
      end
      p.default_reply = ""
    end
  
    generals = table.filter(generals, function(g) return not table.contains(selected, g) end)
    room:returnToGeneralPile(generals)
  
    room:askForChooseKingdom(nonlord)
  end



  function m_1v1_logic:broadcastGeneral()--血量置为10血
    local room = self.room
    local players = room.players
  
    for _, p in ipairs(players) do
      assert(p.general ~= "")
      local general = Fk.generals[p.general]
      p.shield = general.shield 
      p.maxHp =general.maxHp<200 and 800 or  general.maxHp
      p.hp = general.hp<200 and 800 or general.hp
      p.atk=general.atk or 300
      p.def=general.def or 200
      p.spd=general.spd or 200
      p.skill_pool=general.skill_pool or {"poke__siguang","jbspoke_penshe","poke__siguang","poke__siguang"}
      room:broadcastProperty(p, "role")     
      room:broadcastProperty(p, "general")
      room:broadcastProperty(p, "kingdom")
      room:broadcastProperty(p, "deputyGeneral")
      room:broadcastProperty(p, "maxHp")
      room:broadcastProperty(p, "hp")
      room:broadcastProperty(p, "shield")
      room:setPlayerMark(p, "@[shuxing]", {
        propertys = {p.atk ,p.def,p.spd,0,0,0,0,0,0},skill=p.skill_pool
      })
      Pe.changePowerlvs(p,nil,{1})
    end
  end


  return m_1v1_logic
end




local m_1v1_rule = fk.CreateTriggerSkill{
  name = "#jbsaola_rule",
  priority = 0.001,
  refresh_events = {fk.Damage,fk.GameStart},
  can_refresh = function(self, event, target, player, data)
    return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      local players=room.alive_players
      room:doBroadcastNotify("ShowToast", "战斗开始")
      --[[
      room.logic:trigger("fk.Debut", player, player.general, false)
      room.logic:trigger("fk.Debut", player.next, player.general, false)
      if room:getSettings('enableFreeAssign') then---开启自由选将时结束
        room:doBroadcastNotify("ShowToast", "该模式不支持自由选将！")
        --room:gameOver("lord")
        return
      end
      --]]
      --执行奖惩

    end


  end,
}








local m_1v1_mode = fk.CreateGameMode{
  name = "jbsaola_mode",
  minPlayer = 2,
  maxPlayer = 2,
  rule = m_1v1_rule,
  logic = m_1v1_getLogic,
  surrender_func = function(self, playedTime)
    return { { text = "time limitation: 2 min", passed = playedTime >= 120 } }
  end,
}
-- extension:addGameMode(m_1v1_mode)
Fk:loadTranslationTable{
  ["jbsaola_mode"] = "pokemon",
  [":jbsaola_mode"] = desc_1v1,
  ["@jbsfire_buff"]="",
  ["@jbsfire_assignment"]="任务",
  ["@[:]jbsfire_rule"]="模式",
  ["jbsaola-intro"]="奥拉1v1",
  [":jbsaola-intro"]=[[该模式下双方都有十滴血。]],
  ["jbspoke_pohuai"]="破坏死光",
  ["jbspoke_penshe"]="喷射火焰",
  ["#pokeskill-choose"]="选择一个技能并使用！",
  ["@[shuxing]"] = "属性",

}

return m_1v1_mode
